package com.atguigu.executor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@EnableConfigurationProperties(MyThreadPoolProperties.class)
@Configuration
public class MyThreadPoolConfig {
    @Autowired
    private MyThreadPoolProperties myProperties;

    @Bean
    public ThreadPoolExecutor myExecutor(){
        /**
         * 高并发场景下 150线程进来
         *  16个线程会立即得到处理
         *  扩展16个线程继续处理
         *  100个进入到队列当中
         *  剩余的18个会采取拒绝策略
         * 采用那种存放队列
         *  LinkedBlockingQueue
         *      链表不会出现内存不连续问题
         *  ArrayBlockingQueue
         *      空间碎片问题，导致内存空间就不连续
         */
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(myProperties.getCorePoolSize(),
                myProperties.getMaximumPoolSize(),
                myProperties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(myProperties.getQueueLength()),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        return threadPoolExecutor;
    }
}
